/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.*;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import pojo.ThongTinTaiKhoanMail;

/**
 *
 * @author DQ
 */
public class ThongTinTaiKhoanMailDAO {

    public static String layDuongDanTapTin() {
        return "ThongTinTaiKhoan.xml";
    }

    public static ThongTinTaiKhoanMail layThongTinTaiKhoanMail() {
        ThongTinTaiKhoanMail mail = new ThongTinTaiKhoanMail();
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.parse(layDuongDanTapTin());

            //Element root = doc.getRootElements();
            Element root = doc.getDocumentElement();
            NodeList list = root.getChildNodes();
            for (int i = 0; i < list.getLength(); i++) {
                Node node = list.item(i);


                if (node instanceof Element) {
                    Element ele = (Element) node;
                    Text textNode = (Text) ele.getFirstChild();
                    String content = textNode.getData();
                    switch (ele.getTagName()) {
                        case "email":
                            mail.setEmail(content);
                            break;
                        case "password":
                            mail.setPassword(content);
                            break;
                        case "popserver":
                            mail.setPop(content);
                            break;
                        case "smtpserver":
                            mail.setSmtp(content);
                            break;
                        case "popport":
                            mail.setPopport(Integer.parseInt(content));
                            break;
                        case "smtpport":
                            mail.setSmtpport(Integer.parseInt(content));
                            break;
                        case "cheDoXoaMail":
                            mail.setCheDoXoaMail(Integer.parseInt(content));
                            break;

                    }


                }

            }

        } catch (ParserConfigurationException | SAXException | IOException | DOMException | NumberFormatException e) {
        }

        return mail;

    }

    public static void capNhatThongTinEmail(ThongTinTaiKhoanMail mail) throws TransformerConfigurationException, TransformerException, ParserConfigurationException, IOException, SAXException {

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(layDuongDanTapTin());
        Element root = doc.getDocumentElement();
        NodeList list = root.getChildNodes();
        for (int i = 0; i < list.getLength(); i++) {
            Node node = list.item(i);


            if (node instanceof Element) {
                Element ele = (Element) node;
                Text textNode = (Text) ele.getFirstChild();
                String content = textNode.getData();
                switch (ele.getTagName()) {
                    case "email":
                        ele.setTextContent(mail.getEmail());
                        //ele.setTextContent(ch.getNoiDungCauHoi());
                        break;
                    case "password":
                        ele.setTextContent(mail.getPassword());
                        break;
                    case "popserver":
                        ele.setTextContent(mail.getPop());
                        break;
                    case "smtpserver":
                        ele.setTextContent(mail.getSmtp());
                        break;
                    case "popport":
                        ele.setTextContent(Integer.toString(mail.getPopport()));
                        break;
                    case "smtpport":
                        ele.setTextContent(Integer.toString(mail.getSmtpport()));
                        break;
                    case "cheDoXoaMail":
                        ele.setTextContent(Integer.toString(mail.getCheDoXoaMail()));
                        break;



                }
            }

        }
        //Ghi file

        ghiFile(doc);
    }

    
    
    private static void ghiFile(Document doc) throws TransformerConfigurationException, TransformerException {

        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource source = new DOMSource(doc);

        StreamResult result = new StreamResult(layDuongDanTapTin());

        transformer.transform(source, result);

    }
}
